<?php

namespace app\admin\controller;

use library\Controller;
use library\tools\Data;
use think\Db;

/**
 * 审批流管理
 * Class User
 * @package app\admin\controller
 */
class Check extends Controller
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'Check';

    /**
     * 审批流管理
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '审批流管理';
        $query = $this->_query($this->table);
        $query->where(['is_deleted' => '0'])->page();
    }

    /**
     * 添加审批流
     * @auth true
     */
    public function add()
    {
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑审批流
     * @auth true
     */
    public function edit()
    {
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }



    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _form_filter(&$data)
    {
        //获取数据库所有表名
        $sql='show tables';
        $result = Db::query($sql);
        //筛选数据，转换为一维数组
        $this->tables = array_reduce($result, function ($result, $value) {
                return array_merge($result, array_values($value));
            }, array());
        if(request()->isPost()){
            $data['one_check'] = (isset($data['one_check']) && is_array($data['one_check'])) ? join(',', $data['one_check']) : '';
            $data['two_check'] = (isset($data['two_check']) && is_array($data['two_check'])) ? join(',', $data['two_check']) : '';
            $data['three_check'] = (isset($data['three_check']) && is_array($data['three_check'])) ? join(',', $data['three_check']) : '';
            //禁止添加重复表名
            if(!isset($data['id'])){
                if(Db::name('check')->where('is_deleted', 0)->where('table', $data['table'])->find()){
                    $this->error('该表的审批流已存在');
                }
            }
        }else{
            $data['one_check'] = explode(',', isset($data['one_check']) ? $data['one_check'] : '');
            $data['two_check'] = explode(',', isset($data['two_check']) ? $data['two_check'] : '');
            $data['three_check'] = explode(',', isset($data['three_check']) ? $data['three_check'] : '');
            $where = [
                ['is_deleted', '=', 0],
                ['status', '=', 1],
                ['id', '<>', 10000],
            ];
            $this->users = Db::name('system_user')->where($where)->order('create_at desc')->select();
        }
        
    }

    /**
     * 禁用审批流
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '0']);
    }

    /**
     * 启用审批流
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 删除审批流
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->applyCsrfToken();
        $this->_delete($this->table);
    }

    public function get_susers()
    {
        //成员信息列表
        $where = [
            ['is_deleted', '=', 0],
            ['status', '=', 1],
            ['id', '<>', 10000],
        ];
        $users = Db::name('system_user')->where($where)->order('create_at desc')->select();
        $this->success('获取成功！', $users);
    }

}
